package com.yonyougov.devops.cruxcisupport.appport;

import com.yonyougov.devops.cruxcisupport.appport.dto.AppPortAddRequestDto;
import com.yonyougov.devops.cruxcisupport.appport.dto.AppPortDeleteRequestDto;
import com.yonyougov.devops.cruxcisupport.appport.dto.AppPortRequestDto;
import com.yonyougov.devops.cruxcisupport.appport.dto.AppPortUpdateRequestDto;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "应用内部端口模块", tags = "应用内部端口模块")
@RestController
@RequestMapping("/api/v2/appport")
public class AppPortController extends CrudController<AppPortEntity, BigInteger> {

    private final AppPortService appPortService;

    private final AppPortDao appPortDao;

    public AppPortController(AppPortDao appPortDao, AppPortService appPortService) {
        this.appPortDao = appPortDao;
        this.appPortService = appPortService;
    }

    @Override
    public BaseService<AppPortEntity, BigInteger> getService() {
        return appPortService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "前端应用内部端口新增调用", notes = "新增一个应用内部端口")
    @SneakyThrows
    @PostMapping(value = "/ap/add")
    public BaseResponseDto addAppPort(@RequestBody @Valid AppPortAddRequestDto requestDto) {
        AppPortEntity appPortEntity = new AppPortEntity();
        BeanUtils.copyProperties(requestDto, appPortEntity);
        appPortEntity.setId(new CruxDevOpsIdGenerator().generatorId(appPortDao));
        appPortDao.insert(appPortEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端应用内部端口删除调用", notes = "删除一个应用内部端口")
    @PostMapping(value = "/ap/delete/id")
    public BaseResponseDto deleteAppPortById(@RequestBody @Valid AppPortDeleteRequestDto requestDto) {
        AppPortEntity appPortEntity = new AppPortEntity();
        BeanUtils.copyProperties(requestDto, appPortEntity);
        appPortDao.deleteByPrimaryKey(appPortEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端应用内部端口修改调用", notes = "更改一个应用内部端口")
    //<!--用户自定义代码结束-->
    @PostMapping(value = "/ap/update")
    public BaseResponseDto updateAppPort(@RequestBody @Valid AppPortUpdateRequestDto requestDto) {
        AppPortEntity appPortEntity = new AppPortEntity();
        BeanUtils.copyProperties(requestDto, appPortEntity);
        appPortDao.updateByPrimaryKey(appPortEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端-应用-内部端口列表调用", notes = "查询一个应用的全部内部端口")
    @SneakyThrows
    @PostMapping(value = "/ap/app/all")
    public BaseResponseDto findEnvPageLike(@RequestBody @Valid AppPortRequestDto requestDto) {
        AppPortEntity appPortEntity = new AppPortEntity();
        BeanUtils.copyProperties(requestDto, appPortEntity);
        Pagination<AppPortEntity> datas = appPortService.findAllBy(appPortEntity);
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", datas.getDatas());
        result.put("totalCount", datas.getTotalCount());
        return result;
    }
    //<!--用户自定义代码结束-->
}
